//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CommunityServer.Galleries;
using CommunityServer.Galleries.Components;
using CommunityServer.Galleries.Controls;
using CA = ComponentArt.Web.UI;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;

namespace CommunityServer.ControlPanel.Photos
{
	/// <summary>
	///		Summary description for CategoryListControl.
	/// </summary>
	public class CategoryListControl : BaseGalleryGridControl
	{

		#region Child Controls

		protected ResourceControl Resourcecontrol1;
		protected ParentCategoryDropDown CategoryList;
		protected PlaceHolder ParentAlbumContainer;

		protected LinkButton New;
		protected CA.Grid Grid1;
		protected ResourceControl FeedbackFilterLabel;
		protected Modal Modal1;
		protected ModalLink NewCategory;

		#endregion
	

		protected int categoryID
		{
			get
			{
				CSContext cntx = CSContext.Current;
				return cntx.GetIntFromQueryString("categoryid", cntx.GetIntFromQueryString("cid",-1));		
			}
		}

		private void Page_Load(object sender, EventArgs e)
		{
			NewCategory.Text = ControlPanel.Components.ResourceManager.GetString("CP_Photos_Categories_AddNewCategory"); 
			NewCategory.Url = GalleryUrls.Instance().ControlPanel_Photos_CategoryForm_New(this.GalleryPage.CurrentGallery.SectionID, -1, Globals.SafeInt(CategoryList.SelectedValue, -1)); 
			
			if(!Page.IsPostBack && !GalleryPage.IsCallBack)
			{
				BindFilters(); //Populate the Category drop list
				ConfigurePage();//Select the Category in the drop list

				buildGrid();
				Grid.DataBind();
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
			this.Grid.DeleteCommand += new CA.Grid.GridItemEventHandler(this.Grid_DeleteCommand);
			this.CategoryList.SelectedIndexChanged += new EventHandler(this.CategoryList_SelectedIndexChanged);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new EventHandler(this.Page_Load);
		}
		#endregion

		private int DeleteCategory(int categoryID)
		{
			if(categoryID > 0)
			{
						PostCategory pc = PostCategories.GetCategory(categoryID,this.CurrentGallery.SectionID,true, true);
						PostCategories.DeleteCategory(pc.CategoryID,pc.SectionID);
						if(pc.SectionID != CurrentGallery.SectionID)
							throw new CSException(CSExceptionType.SectionNotFound);
						if(pc != null)
							PostCategories.DeleteCategory(pc.CategoryID,pc.SectionID);
			}

			return 1;
		}


		private void BindFilters()
		{
			ArrayList al = PostCategories.GetCategories(CurrentGallery.SectionID, false, true, -1);
			
			foreach(PostCategory pc in al)
				CategoryList.Items.Add(new ListItem(Server.HtmlDecode(pc.Name) ,pc.CategoryID.ToString()));

			CategoryList.Items.Insert(0,new ListItem(ResourceManager.GetString("CP_Photos_CategoryFilterAll"),"-1"));
		}

		private void ConfigurePage()
		{

			ListItem li = CategoryList.Items.FindByValue(categoryID.ToString());
			
			if(li != null)
				li.Selected = true;

			if (CurrentGallery.CategorizationType != CategorizationType.Album)
			{
				ParentAlbumContainer.Visible = false;
				Grid.Levels[0].Columns[0].Visible = false;
			}
              
		}

		protected override void buildGrid()
		{
			this.CurrentGridRowSize = 2;

			ArrayList al = GalleryPostCategories.GetCategories(CurrentGallery.SectionID, true, true, categoryID, true);	
			
			Grid.DataSource = al;

			this.RecordCount = al.Count;

			base.buildGrid();

		}

		protected override void ConfigureGrid()
		{
			AddGridCheckMarkTemplate("IsEnabled");
			AddGridHrefTemplate(@"postlist.aspx?cid=##  DataItem.GetMember(""CategoryID"").Text ##", "TotalThreads") ;
			AddGridHrefTemplate(@"javascript:Telligent_Modal.Open(""CategoryForm.aspx?sectionid=##  DataItem.GetMember(""SectionID"").Text ##&categoryID=## DataItem.GetMember(""CategoryID"").Text ##"", 400, 300, reloadCategories);", "Name") ;
			AddGridPagerTemplate("Name", "CategoryID", "MostRecentPostDate","IsEnabled");
		}


		private void Grid_DeleteCommand(object sender, CA.GridItemEventArgs e)
		{
			DeleteCategory(Globals.SafeInt(e.Item["CategoryID"].ToString(), 0)); 
		}

		private void CategoryList_SelectedIndexChanged(object sender, EventArgs e)
		{
			//NewCategory.Url = String.Format("CategoryForm.aspx?SectionID={0}&categoryID={1}", this.GalleryPage.CurrentGallery.SectionID, CategoryList.SelectedValue); 
			//Grid.CurrentPageIndex = 0;
			//Grid.DataBind();

			string url  = "{0}?cid={1}";
			url = string.Format(url,Request.Path,Globals.SafeInt(CategoryList.SelectedValue,-1));
			Response.Redirect(url);

		}
	}
}